var time=require("../../../utils/util.js")
var proportion=0;
var maxmoney=[];
var flagTime=0;
var max=10;
var sendListen=0;
var Imgs=[];
var voiceTime;
var index;
var voiceurl;
var imgurl=[];
var answerId;
var wxNum;
var nowmoney=10;
var id;
var uuid;
var Index;
var figureTime;
var give=0;                   //最终给的                
for(var i=0;i<=max;i++){
  maxmoney.push(i);
}
Page({
  data: {

    question: "",
    introduce: "是临沂市第四人民医院副院长，临沂市网络成瘾戒治中心（简称临沂网戒中心）主任",
    givemoney: "￥0",
    answers: [],
    wait:"",
    maxmoney:maxmoney,
    nowmoney:max,
    choosemoney:"none",
    zan:"zan",
    userImg:"",
    ImgNum:"",
    isSolved:"",
    showquestion:"",
    imgDisplay:"display",
    zanImg: "../../image/zan.png",
    showQue: 'none',
    NoshowQUe: 'display'
    

   
    
  },
  addObject: function (a, b) {
    a = JSON.stringify(a);
    b = JSON.stringify(b);
    a = a.concat('。');
    a = a.replace('}。', "");
    b = b.replace('{', ",");
    a = a.concat(b);
    a = JSON.parse(a);
    return a;
  },
  givestar: function (e) {
    
    var index=e.currentTarget.id;
    var data=this.data.answers;
    data[index].starImg='../../image/star1.png';
    data[index].starFun="";
    this.setData(
      {
        answers:data
      }
    );
    wx.showToast({
      title: '评价成功！',
      icon:'success',
      duration:800
    })
    var id=this.data.answers[index].id;
    
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/starAnswer",
      data: {
        answerId: id,
        
      },
      success: function () {
        console.log("点星星成功！");
      },
    })

  },
  toExpert: function (e) {
    var expert = e.currentTarget.id;
    expert = this.data.answers[expert].master.id;
    wx.navigateTo({
      url: '../../experts/information/information?id=' + expert,
    })
  },
  addZan: function (wxNum) {
    /*this.data.answers[index].praiseUserSet.length++;
    console.log("点赞");

    this.setData(
      {
        answers: this.data.answers,
        zan: "",
      
      }
    );*/
    console.log(answerId, wxNum)
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/praise",
      data: {
        answerId: answerId,
        wxNumber: wxNum
      },
      success: function (res) {
        console.log("11111111"+res.data);
      },
    })
  },
  addObject: function (a, b) {
    a = JSON.stringify(a);
    b = JSON.stringify(b);
    a = a.concat('。');
    a = a.replace('}。', "");
    b = b.replace('{', ",");
    a = a.concat(b);
    a = JSON.parse(a);
    return a;
  },
  zan: function (e) {
    index = e.currentTarget.id;
    this.data.answers[index].praiseImg1 = this.data.answers[index].praiseImg2;
    this.data.answers[index].zanNum++;
    this.data.answers[index].zan="";
    this.setData(
      {
        answers: this.data.answers,
        zan:""
      }
    );
    answerId = this.data.answers[index].id;
    this.Login();
  },
  Login: function () {
    console.log("执行了")
    var that = this;

    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {

            wxNum = res.data;
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data: {
                login_uuId: wxNum
              },
              success: function (res) {
                if (res.data == 'error') {
                  that.tologin();
                }
                else {
                  console.log(wxNum)
                   that.addZan(wxNum)
                }
              },
            })

          },
        })

      },
      fail: function () {
        console.log("fail")
        that.tologin();
      },

    })


    //

  },
  exmine: function (e) {
    var index=e.currentTarget.id;
    var images=this.data.userImg;
    var imgPath=[];
    imgPath.push(images[index]);
    for(var i=0;i<images.length;i++)
    if(i!=index)
    imgPath.push(images[i]);
    wx.previewImage({
      urls: imgPath,
    })
  },
  tologin: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLogin',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("return" + res.data);
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });

              wxNum = res.data;
              that.addZan(wxNum);


            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  listenNum: function (answerId) {
    sendListen = 1;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/listen',
      data: {
        answerId: answerId
      },
      method: 'GET',
      success: function () {

      },
    })
  },
  listen:function(e){
    
    var index=e.currentTarget.id;
    Index=index;
    var answer=this.data.answers;
    if (sendListen == 0)
      this.listenNum(answer[index].id);
    voiceTime=answer[index].recordTime;
    var url=this.data.answers[index].recordUrl;
    var that=this;
    var font=answer[index].f5;

    

    for(var i=0;i<answer.length;i++){
      if(i!=index){
        answer[i].f5="点击播放";
        that.setData(
          {
            answers:answer
          }
        );
      }
    }
    if(font=="点击播放")
   {
     answer[index].f5='缓冲中...';
     that.setData(
       {
         answers:answer
       }
     );
    wx.downloadFile({
      url:url,
      success:function(res){
        voiceurl=res.tempFilePath;
        figureTime = setInterval(that.figureTime, 500);
        answer[index].f5 = "播放中...";
        that.setData(
          {
            answers: answer
          }
        );
        wx.playVoice({
          filePath: voiceurl,
          complete:function(){
            console.log("yes")
          },
        })
       
      }
    })
  }
    if(font=="播放中..."){
      wx.stopVoice();
      clearInterval(figureTime);
      flagTime=0;
      answer[index].f5="点击播放";
      that.setData(
        {
          answers:answer
        }
      );
    }
    /*if(font=="已暂停播放"){
      wx.playVoice({
        filePath: voiceurl,
      })
      figureTime=setInterval(that.figureTime,500);
      answer[index].f5="播放中...";
      that.setData(
        {
          answers:answer
        }
      );
    }*/
  },
 figureTime:function(){
   flagTime=flagTime+0.5;
   if(flagTime>=voiceTime){
     this.data.answers[Index].f5="点击播放";
     flagTime=0;
     clearInterval(figureTime);
     wx.removeSavedFile({
       filePath: voiceurl,
     })
     this.setData(
       {
         answers:this.data.answers
       }
     );
   }
 },
 prewImg: function (e) {
   var index1 = e.currentTarget.id;
   var index2 = e.target.dataset.index;

   imgurl.push(this.data.answers[index1].answerImgSet[index2].imgUrl);
   for (var i = 0; i < this.data.answers[index1].answerImgSet.length; i++) {
     if (i != index2)
       imgurl.push(this.data.answers[index1].answerImgSet[i].imgUrl);
   }
   wx.previewImage({
     urls: imgurl,
   })
   imgurl = []

 },
  cancelmoney:function(){
    this.setData(
      {
        choosemoney:"none"
      }
    );
  },
  confirmmoney:function(){
    var that=this;
    var times=Date();
   
    this.setData(
      {
        choosemoney:"none"
      }
    );
    wx.request({
      url: "",
      data:{
          //answerId:,      答案的ID
          //money:,         钱数
          //getDate:        得到的时间

      },
      success:function(){
        //提示给钱成功
      },
      fail:function(){
        //提示给钱失败
      },
    })
  },
  givemoney:function(){
    this.setData(
      {
        choosemoney:"display"
      }
    );
  },
  choosemoney:function(e){
      var money=e.detail.value;
      money=maxmoney[money];
      give=money;
      this.setData(
        {
          nowmoney:max-give
        }
      );
      
  },

 
  
  exmine: function () {
    wx.previewImage({
      // current: 'String', // 当前显示图片的链接，不填则默认为 urls 的第一张
      urls: Imgs,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  Loadquestion:function(uuid){
    var that = this;
    console.log('调用的我');
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByIdOrUuName",
      data: {
        
        uuName:uuid,
        
      },
      method:"GET",
      success: function (res) {   //获取问题
        console.log(res.data)
        Imgs = res.data.questionImgUrl;
        if (Imgs.length == 0) {
          that.setData(
            {
              imgDisplay: "none"
            }
          );
        }
        else {
          that.setData(
            {
              imgDisplay: "display"
            }
          );
        }
        if (res.data.isSolved == '是') {
          res.data.isSolved = "已解答";
          that.setData(
            {
              wait: "none",
              showquestion: "display"

            }
          );
        }
        else if (res.data.isSolved != '已关闭') {
          res.data.isSolved = "未解答";
          that.setData(
            {
              wait: "display",
              showquestion: "none",
              wait_text: "等待专家为您解答",
              wait_bgcolor: 'white'
            }
          );
        }
        else {
          res.data.isSolved = '已关闭';
          that.setData(
            {
              wait: "display",
              showquestion: "none",
              wait_text: "",
              wait_bgcolor: "gainsboro"
            }
          );
        }
        console.log('添加对象前', res.data.answerList);
        for (var i = 0; i < res.data.answerList.length; i++) {
          res.data.answerList[i].praiseImg1 = '../../image/zan.png';
          res.data.answerList[i].praiseImg2 = '../../image/zan1.png';
          var zanFun = { 'zan': 'zan' };
          var zanNum = res.data.answerList[i].praiseUserSet.length;
          var zanNum = { zanNum: zanNum };
          res.data.answerList[i] = that.addObject(res.data.answerList[i], zanNum);
          res.data.answerList[i] = that.addObject(res.data.answerList[i], zanFun);
          if (res.data.answerList[i].starNum > 0) {
            console.log('以评价完');
            var insert = { 'starImg': '../../image/star1.png', 'starFun': "" };
            res.data.answerList[i] = that.addObject(res.data.answerList[i], insert);

          }
          else {
            console.log('暂未评价')
            var insert = { 'starImg': '../../image/star.png', 'starFun': "givestar" };
            res.data.answerList[i] = that.addObject(res.data.answerList[i], insert);
          }
          var timeNow = Date.now();
          var times = res.data.answerList[i].answerTime;
          times = timeNow - times;
          times = parseInt(times / 60000);
          if (times > 60) {
            if (times > 1440)
              times = parseInt(times / 1440) + '天';
            else
              times = parseInt(times / 60) + '小时';
          }
          else {
            times = times + '分钟';
          }
          res.data.answerList[i].answerTime = times;
          res.data.answerList[i].f5 = "点击播放";
          if (res.data.answerList[i].recordUrl == "")
            res.data.answerList[i].recordDisplay = "none";
          else {
            res.data.answerList[i].recordDisplay = "display";
          }
        }
        for (var i = 0; i < res.data.answerList.length; i++) {
          if (res.data.answerList[i].info == "")
            res.data.answerList[i].textDisplay = "none";
          else {
            res.data.answerList[i].textDisplay = "display";
          }
        }
        for (var i = 0; i < res.data.answerList.length; i++) {
          if (res.data.answerList[i].answerImgSet.length == 0)
            res.data.answerList[i].imageDisplay = "none";
          else {
            res.data.answerList[i].imageDisplay = "display"
          }
        }
        console.log(res.data.answerList, '添加对象后')
        that.setData(
          {
            question: res.data.info,
            userImg: res.data.questionImgUrl,
            ImgNum: res.data.questionImgUrl.length,
            answers: res.data.answerList,
            isSolved: res.data.isSolved,
            NoshowQue: 'none',
            showQue: 'display'
          }
        );
            /*if (res.detail.answers == []) {
              that.setData(
                {
                  wait: "display"
                }
              );
            }
            else {
              that.setData(
                {
                  wait: "none"
                }
              );
            }*/
      },
      fail: function () {

      },
    })
    this.setData(
      {
        question: ""
      }
    );
  },
  Loadquestion1: function (uuid) {
    var that = this;
    console.log('调用的我1',uuid);
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
        console.log('缓存',res.data);
        wx.request({
          url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByIdOrUuName",
          data: {
            
            id: uuid,
            useruuid:res.data

          },
          method: "GET",
          success: function (res) {   //获取问题
            console.log(res.data)
            Imgs = res.data.questionImgUrl;
            if (Imgs.length == 0) {
              that.setData(
                {
                  imgDisplay: "none"
                }
              );
            }
            else {
              that.setData(
                {
                  imgDisplay: "display"
                }
              );
            }
            if (res.data.isSolved == '是') {
              res.data.isSolved = "已解答";
              that.setData(
                {
                  wait: "none",
                  showquestion: "display"

                }
              );
            }
            else if (res.data.isSolved != '已关闭') {
              res.data.isSolved = "未解答";
              that.setData(
                {
                  wait: "display",
                  showquestion: "none",
                  wait_text: "等待专家为您解答",
                  wait_bgcolor: 'white'
                }
              );
            }
            else {
              res.data.isSolved = '已关闭';
              that.setData(
                {
                  wait: "display",
                  showquestion: "none",
                  wait_text: "",
                  wait_bgcolor: "gainsboro"
                }
              );
            }
            console.log('添加对象前',res.data.answerList);
            for (var i = 0; i < res.data.answerList.length; i++) {
              res.data.answerList[i].praiseImg1 = '../../image/zan.png';
              res.data.answerList[i].praiseImg2 = '../../image/zan1.png';
              var zanFun = { 'zan': 'zan' };
              var zanNum = res.data.answerList[i].praiseUserSet.length;
              var zanNum = { zanNum: zanNum };
              res.data.answerList[i] = that.addObject(res.data.answerList[i], zanNum);
              res.data.answerList[i] = that.addObject(res.data.answerList[i], zanFun);
              if(res.data.answerList[i].starNum>0){
                console.log('以评价完');
                var insert={'starImg':'../../image/star1.png','starFun':""};
                res.data.answerList[i]=that.addObject(res.data.answerList[i],insert);

              }
              else{
                console.log('暂未评价')
                var insert={'starImg':'../../image/star.png','starFun':"givestar"};
                res.data.answerList[i] =that.addObject(res.data.answerList[i],insert);
              }
              var timeNow = Date.now();
              var times = res.data.answerList[i].answerTime;
              times = timeNow - times;
              times = parseInt(times / 60000);
              if (times > 60) {
                if (times > 1440)
                  times = parseInt(times / 1440) + '天';
                else
                  times = parseInt(times / 60) + '小时';
              }
              else {
                times = times + '分钟';
              }
              res.data.answerList[i].answerTime = times;
              res.data.answerList[i].f5 = "点击播放";
              if (res.data.answerList[i].recordUrl == "")
                res.data.answerList[i].recordDisplay = "none";
              else {
                res.data.answerList[i].recordDisplay = "display";
              }
            }
            for (var i = 0; i < res.data.answerList.length; i++) {
              if (res.data.answerList[i].info == "")
                res.data.answerList[i].textDisplay = "none";
              else {
                res.data.answerList[i].textDisplay = "display";
              }
            }
            for (var i = 0; i < res.data.answerList.length; i++) {
              if (res.data.answerList[i].answerImgSet.length == 0)
                res.data.answerList[i].imageDisplay = "none";
              else {
                res.data.answerList[i].imageDisplay = "display"
              }
            }
            console.log(res.data.answerList,'添加对象后')
            that.setData(
              {
                question: res.data.info,
                userImg: res.data.questionImgUrl,
                ImgNum: res.data.questionImgUrl.length,
                answers: res.data.answerList,
                isSolved: res.data.isSolved,
                NoshowQue: 'none',
                showQue: 'display'
              }
            );
            /*if (res.detail.answers == []) {
              that.setData(
                {
                  wait: "display"
                }
              );
            }
            else {
              that.setData(
                {
                  wait: "none"
                }
              );
            }*/
          },
          fail: function () {

          },
        })
      },
    })
   
    this.setData(
      {
        question: ""
      }
    );
  },
  Check:function(){
      //获取点赞人的微信号 检测是否为已点赞用户
      
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    
    if(options.uuid.length>6)
    this.Loadquestion(options.uuid);
    else
    this.Loadquestion1(options.uuid);
    this.Check();

  },

})